package com.example.a16478082_jibuqi;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class JibuqiActivity extends AppCompatActivity implements View.OnClickListener, SensorEventListener {
    private SensorManager sManager;
    private Sensor mSensorAccelerometer;
    private TextView tv_step;
    private Button btn_start;
    private int step=0;       //步数
    private double oriValue=0;  //原始值
    private double lstValue=0;  //上次的值
    private double curValue=0;   //当前值
    private Boolean motiveState=true;  //是处于运动状态
    private Boolean processState=false;  //标记当前是否已经在计步


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jibuqi_main);
        sManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        mSensorAccelerometer=sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sManager.registerListener(this,mSensorAccelerometer,SensorManager.SENSOR_DELAY_UI);
        bindViews();
    }

    private void bindViews() {
        tv_step=(TextView)findViewById(R.id.tv_step);
        btn_start=(Button)findViewById(R.id.btn_start);
        btn_start.setOnClickListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        double range=1;  //设定一个精确范围
        float [] value=sensorEvent.values;
        curValue=magnitude(value[0],value[1],value[2]);  //计算当前的模
        // 向上加速的状态
        if (motiveState==true){
            if(curValue>lstValue) lstValue=curValue;
            else{
                // 检测一次峰值
                if(Math.abs(curValue-lstValue)>range){
                    oriValue=curValue;
                    motiveState=false;
                }
            }
        }

        // 向下加速的状态
        if(motiveState==true){
            if(curValue<lstValue) lstValue=curValue;
            else {
                // 检测一次峰值
                oriValue=curValue;
                if(processState==true){
                    step++;   //步数+1
                    if(processState==true){
                        tv_step.setText(step+"");  // 读数更新
                    }
                    motiveState=true;
                }
            }
        }
    }

    private double magnitude(float x, float y, float z) {
        double magnitude=0;
        magnitude=Math.sqrt(x*x+y*y+z*z);
        return magnitude;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    public void onClick(View view) {
        step=0;
        tv_step.setText("0");
        if(processState==true){
            btn_start.setText("开始");
            processState=false;
        }else{
            btn_start.setText("停止");
            processState=true;
        }
    }
}
